package org.example;

import lombok.extern.slf4j.Slf4j;

import javax.xml.crypto.dsig.spec.HMACParameterSpec;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
public class MapExample {
private static Map<Integer,Integer> map = new HashMap<>();
    // 请求总数
    public static int clientTotal = 5000;

    // 同时并发执行的线程数
    public static int threadTotal = 200;

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        for (int i = 0; i < clientTotal ; i++) {
            final int threadTotal=i;
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    add(threadTotal);
                    semaphore.release();
                } catch (Exception e) {
                    log.error("exception", e);
                }
            });
        }
        executorService.shutdown();
        log.info("count:{}", map.size());
    }

    private static void add(int threadTotal) {
        map.put(threadTotal,threadTotal);
    }
}